package com.cennac.ccblog.web;

import com.cennac.ccblog.entity.User;
import com.cennac.ccblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by Cennac on 16/1/25.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = {"list", ""})
    public String list( Model model) {

        List<User> userList=userService.selectAll();
        model.addAttribute("userList", userList);
        return "user_list";
    }

    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String add(@ModelAttribute("user") User user) {
        return "user_add";
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String addUser(@ModelAttribute("user") User user) {
        userService.addUser(user);
        return "redirect:/user/list";
    }
}